import java.util.*;

public class EventsManager {

/*~~~~ ATTRIBUTES ~~~~*/

	protected int intENDParsers;
	protected int intTOTParsers;
	protected Vector<Event> objEvents;


/*~~~~ CONSTRUCTORS ~~~~*/

	public EventsManager(int intParsers) {
		this.intTOTParsers = intParsers;
		this.objEvents = new Vector<Event>();
	}


/*~~~~ PUBLIC METHODS ~~~~*/

	public synchronized Event getObjEventValue() throws InterruptedException {
		Event objEvent;
		while (objEvents.isEmpty()){
			if (intTOTParsers == intENDParsers) return null;
			wait();
		}
		objEvent = objEvents.get(0);
		objEvents.remove(0);
		return objEvent;
	}

	public synchronized void setObjEventsValue(Event objEvent) {

		/* Null elements will not be wrote */
		if (objEvent != null) { 
			objEvents.add(objEvent);
			notifyAll();
		}
	}


	public synchronized void tellYouEnded() {
		this.intENDParsers++;
		if (intTOTParsers == intENDParsers && objEvents.isEmpty()) {
			forceEnd();
		}
	}


	public void forceEnd() {
		objEvents.add(null);
		notifyAll();
	}

/*~~~~ STATIC METHODS ~~~~*/

    /** <p>This method force the length of a string to <code>intEndSize</code> characters.
     * <li>If current length is less, it adds a sequence of <code>chrAdds</code> character [third argument].<br />
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the forth argument declare if addition will be alternate between begin and end (true = centered)
     * or only at the beginning (false = "right align");
     * <li>If current length is more, it takes only firsts <code>intEndSize</code> characters.</p> **/
	public static String strFormat(String strOrig, int intEndSize, char chrAdds, boolean blnCenter){
		if (strOrig.length() < intEndSize){
			int zeri = intEndSize - strOrig.length();

			/* Se si vuole centrare il testo */
			if (blnCenter) {
				boolean start = true;
				
				for (int i = 0; i < zeri; i++) {
					if (start) {
						strOrig = chrAdds + strOrig;
						start = false;
					} else {
						strOrig += chrAdds;
						start = true;
					}
				}
				
			/* Se si vuole "allineare a destra" */
			} else {
				for (int i = 0; i < zeri; i++)
					strOrig = chrAdds + strOrig;
			}
			
		} else if (strOrig.length() > intEndSize){
			String temp = new String("");
			
			for (int i = 0; i < intEndSize; i++)
				temp += strOrig.charAt(i);
			
			strOrig = temp;
		}
		
		return strOrig;
	} /**/
	
    /** <p>This method force the length of a string to <code>intEndSize</code> characters.
     * <li>If current length is less, it adds a sequence of zero at the beginning;
     * <li>If current length is more, it takes only firsts <code>intEndSize</code> characters.</p> **/
	public static String strFormat(String strOrig, int intEndSize){
		return strFormat(strOrig, intEndSize, '0', false);
	} /**/
	
    /** <p>This method force the length of a string to <code>intEndSize</code> characters.
     * <li>If current length is less, it adds a sequence of <code>chrAdds</code> character [third argument].<br />
     *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alternating between begin and end (centered)
     * or only at the beginning (false = "right align");
     * <li>If current length is more, it takes only firsts <code>intEndSize</code> characters.</p> **/
	public static String strFormat(String strOrig, int intEndSize, char chrAdds){
		return strFormat(strOrig, intEndSize, chrAdds, true);
	} /**/


	/** <p>This method delete all non-numerical characters from a string</p>
	 *  <p>Other than numbers, accepts only a <code>minus</code> [-], if found for first,
	 *  and first <code>dot</code> [.] ever found. **/
	public static String numberize(String strOrig){
		boolean decimal = false;
		String strOutput = "";

		for (int i = 0; i < strOrig.length(); i++){
			/* Riporto solo caratteri numerici, ma se trovo un segno meno prima di
			 * qualsiasi altro carattere numerico, lo accetto */
			if (((int)strOrig.charAt(i) > 47 && (int)strOrig.charAt(i) < 58) ||
				   (strOutput.length() == 0 && (int)strOrig.charAt(i) == 45) ||
				   ((int)strOrig.charAt(i) == 46 && !decimal)){
				strOutput += strOrig.charAt(i);
				if ((int)strOrig.charAt(i) == 46)
					decimal = true;
			}
		}
		
		if (strOutput == "" || strOutput == "-") strOutput = "0";
		
		return strOutput;
	}
}
